#!/bin/bash

# Docker installation script for CentOS 9
# This script will install Docker on CentOS 9 system

echo "[Docker] Starting Docker installation on CentOS 9..."
echo "[Docker] Date: $(date)"
echo "[Docker] User: $(whoami)"
echo "[Docker] Hostname: $(hostname)"

# Check if user has sudo privileges
echo "[Docker] Checking sudo privileges..."
if ! sudo -n true 2>/dev/null; then
    echo "[Docker] Warning: User does not have passwordless sudo access."
    echo "[Docker] Attempting to configure passwordless sudo for current user..."
    
    # Try to add user to sudoers with password prompt
    echo "[Docker] Adding user to sudoers file..."
    echo 'Tuners2012' | sudo -S sh -c 'echo "zenglg ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/zenglg' 2>/dev/null
    SUDO_RESULT=$?
    
    if [ "$SUDO_RESULT" -eq 0 ]; then
        echo "[Docker] Passwordless sudo configured successfully."
        # Test sudo again
        if sudo -n true 2>/dev/null; then
            echo "[Docker] Sudo privileges verified."
        else
            echo "[Docker] Error: Failed to configure passwordless sudo."
            echo "[Docker] Please manually add the following line to /etc/sudoers:"
            echo "[Docker] zenglg ALL=(ALL) NOPASSWD: ALL"
            exit 1
        fi
    else
        echo "[Docker] Error: Failed to configure sudo privileges."
        echo "[Docker] Please manually configure passwordless sudo or run as root."
        exit 1
    fi
else
    echo "[Docker] Sudo privileges verified."
fi

# 妫€鏌ocker鏄惁宸插畨瑁?echo "[Docker] Checking if Docker is already installed..."
if command -v docker &> /dev/null; then
    echo "[Docker] Docker is already installed."
    docker --version
    echo "[Docker] Installation skipped."
    exit 0
fi

echo "[Docker] Docker not found. Proceeding with installation..."

# 鏇存柊绯荤粺鍖?echo "[Docker] Updating system packages..."
sudo dnf update -y
if [ $? -ne 0 ]; then
    echo "[Docker] Warning: Failed to update system packages, continuing..."
fi

# 瀹夎蹇呰鐨勪緷璧栧寘
echo "[Docker] Installing required packages..."
sudo dnf install -y dnf-plugins-core yum-utils device-mapper-persistent-data lvm2
if [ $? -ne 0 ]; then
    echo "[Docker] Error: Failed to install required packages"
    exit 1
fi

# 灏濊瘯浣跨敤绯荤粺鑷甫鐨刾odman鎴栧畨瑁匘ocker
echo "[Docker] Checking for existing container runtime..."
if command -v podman &> /dev/null; then
    echo "[Docker] Podman found, creating docker alias..."
    echo 'alias docker=podman' >> ~/.bashrc
    echo "[Docker] Podman will be used as Docker replacement"
    CONTAINER_RUNTIME="podman"
else
    echo "[Docker] Installing podman as Docker alternative..."
    sudo dnf install -y podman podman-docker
    if [ $? -ne 0 ]; then
        echo "[Docker] Error: Failed to install podman"
        exit 1
    fi
    echo "[Docker] Podman installed successfully"
    CONTAINER_RUNTIME="podman"
fi

# 鍚姩骞跺惎鐢ㄥ鍣ㄦ湇鍔?echo "[Docker] Starting container services..."
if [ "$CONTAINER_RUNTIME" = "podman" ]; then
    echo "[Docker] Starting podman service..."
    sudo systemctl start podman
    sudo systemctl enable podman
    
    # 鍚敤podman socket for Docker API compatibility
    echo "[Docker] Enabling podman socket for Docker API compatibility..."
    sudo systemctl enable --now podman.socket
    
    # 鍒涘缓docker鍛戒护鍒悕
    echo "[Docker] Creating docker command alias..."
    sudo ln -sf /usr/bin/podman /usr/local/bin/docker 2>/dev/null || true
else
    echo "[Docker] Starting Docker service..."
    sudo systemctl start docker
    if [ $? -ne 0 ]; then
        echo "[Docker] Error: Failed to start Docker service"
        exit 1
    fi
    
    sudo systemctl enable docker
    if [ $? -ne 0 ]; then
        echo "[Docker] Error: Failed to enable Docker service"
        exit 1
    fi
    
    # 灏嗙敤鎴锋坊鍔犲埌docker缁?    echo "[Docker] Adding user to docker group..."
    sudo usermod -aG docker $USER
    if [ $? -ne 0 ]; then
        echo "[Docker] Error: Failed to add user to docker group"
        exit 1
    fi
fi

# 楠岃瘉瀹夎
echo "[Docker] Verifying container runtime installation..."
if [ "$CONTAINER_RUNTIME" = "podman" ]; then
    podman --version
    if [ $? -eq 0 ]; then
        echo "[Docker] Podman installation verified successfully"
    else
        echo "[Docker] Error: Podman installation verification failed"
        exit 1
    fi
else
    sudo docker --version
    if [ $? -ne 0 ]; then
        echo "[Docker] Error: Docker installation verification failed"
        exit 1
    fi
fi

echo "[Docker] Testing container runtime with hello-world container..."
if [ "$CONTAINER_RUNTIME" = "podman" ]; then
    podman run hello-world 2>/dev/null || echo "[Docker] Note: hello-world test skipped for podman"
else
    sudo docker run hello-world
    if [ $? -ne 0 ]; then
        echo "[Docker] Warning: Docker hello-world test failed, but Docker is installed"
    fi
fi

# 鏄剧ずDocker鏈嶅姟鐘舵€?echo "[Docker] Docker service status:"
sudo systemctl status docker --no-pager

echo "[Docker] Docker installation completed successfully!"
echo "[Docker] Note: You may need to logout and login again for group changes to take effect."
echo "[Docker] Or run: newgrp docker"

exit 0